<?php
// This file is part of CorrectWriting question type - https://code.google.com/p/oasychev-moodle-plugins/
//
// CorrectWriting question type is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CorrectWriting is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CorrectWriting.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Correct writing question answer definition class.
 *
 * @package    qtype
 * @subpackage correctwriting
 * @copyright  2011 Sychev Oleg
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

class qtype_correctwriting_answer extends question_answer {

    /**
     * List of enmerations
     * TODO: Vadim replace with appropriate type
     * @var mixed
     */
    public $enumerations;

    /**
     * Constructor.
     * @param int $id the answer.
     * @param string $answer the answer.
     * @param number $fraction the fraction this answer is worth.
     * @param string $feedback the feedback for this answer.
     * @param int $feedbackformat the format of the feedback.
     * @param string $enumerations list of enumerations
     */
    public function __construct($id, $answer, $fraction, $feedback, $feedbackformat, $enumerations) {
        $this->id = $id;
        $this->answer = $answer;
        $this->fraction = $fraction;
        $this->feedback = $feedback;
        $this->feedbackformat = $feedbackformat;
        $this->enumerations = $this->deserialize_enumerations($enumerations);
    }

    /**
     * Deserializes enumerations
     * @param string $enumerations enumerations, as fetched from database
     * @return mixed
     */
    protected function deserialize_enumerations($enumerations) {
        $enumerations = explode(';', $enumerations);
        $result = array();
        foreach($enumerationss as $value) {
            $result[] = array();
            $value = str_replace("[","",$value);
            $value = str_replace("]","",$value);
            $elements = explode(',', $value);
            foreach($elements as $element) {
                $position = explode('-',$element);
                $result[count($result)][] = [(int)reset($position),(int)end($position)];
            }
        }
        return $result;
    }
}
